gusucode.com > ACELP语音压缩的源代码 > ACELP语音压缩的源代码/Codec8000cpp SDK/Demo/Examples/Coder8.cpp

    //  Example of VIMAS Speech codec DLL using 
//  Copyright VIMAS Technologies,  1998-2000

#include <windows.h>
#include "codec.h"
#include <iostream.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <direct.h>

// The program will create directory "C:\\test_codec", 
// please, delete it after testing 
#define FILE_COMPRESSED "C:\\test_codec\\compres.prm"  // compressed speech file
                      
#define SHORT short int

typedef acelp (WINAPI *InitProc) ( SHORT mode );
typedef void (WINAPI *DeInitProc) ( acelp InterCurrent );
typedef short int (WINAPI *EncoderProc) (  acelp, SHORT *, unsigned char  * , SHORT );

void main(void)
{
	HMODULE hLibrary = LoadLibrary( "VimasCodec.dll " );
	 if( hLibrary==NULL )
	 {
		cout<<"Can not load library\n";
		return;
	 }

	InitProc pInit = (InitProc)GetProcAddress( hLibrary, "Init" );
	 if( pInit==NULL )
	 {
		cout<<" Can not return the address of Init() function. \n";
		return;
	 }

	DeInitProc pDeInit = (DeInitProc)GetProcAddress( hLibrary, "DeInit" );
	 if( pDeInit==NULL )
	 {
		cout<<" Can not return the address of DeInit() function. \n";
		return;
	 }

	EncoderProc pEncoder = (EncoderProc)GetProcAddress( hLibrary, "Encoder" );
	 if( pEncoder==NULL )
	 {
		cout<<" Can not return the address of Encoder() function. \n";
		return;
	 }


	SHORT *xi;
    SHORT VadDecision;
	SHORT 	format;
	long rc;
	long N;
	long NF;
	long num_fr;
	acelp InterCurrent;
    acelp_frame CompressedFrame;
	FILE  *fprm;
	FILE  *fsample;
	char s[30];
 

    cout<<"Input file name\n";
	cin>>s;
	if( (fsample=fopen(s,"rb"))==NULL)
	{
		cout<<"Can not open input file\n";
		return;
	}
	N=filelength(fileno(fsample));
	NF=N/(FRAME_SIZE*sizeof(SHORT)); // number of frames in the file

	xi=new SHORT[FRAME_SIZE];

// Create directory for compressed speech file
  _mkdir( "C:\\test_codec" );


// Encoder initialization	
	InterCurrent=(*pInit)(1);  
    if(!InterCurrent)
		return;

// WAV format recognition 
   fread(&rc,sizeof(long),1,fsample);

   if ( rc==1179011410l )
	   format=1;  // WAV format 
   else
	   format=0;  // others

   fseek( fsample, 0l, SEEK_SET);


// If WAV format recognized....
  if ( format==1 )
  {
// test the wav file parameters.
	SHORT hdr[23];
    // wav header reading 
	fread(hdr,sizeof(SHORT),23,fsample);
	// sampling frequency testing
	if( hdr[12] !=8000 )
	{
	 cout<<"Sampling frequency != 8000 Hz \n  ";
	 return;
	} 
    //bit per sample testing 
	if( hdr[16] !=2 )
	{
	  cout<<" Bits per sample != 16 \n";
	  return;
	}
    // mono or stereo  testing
	if ( hdr[11] !=1 )
	{
		cout<<" Number of channels !=1 \n";
		return;
	}
  }
      
  fprm  = fopen(FILE_COMPRESSED,"wb");


	for( num_fr=0; num_fr<NF-1; num_fr++ )
	{
// read speech frame from file
        fread(xi,sizeof(SHORT),FRAME_SIZE,fsample); 
// encode speech frame
		VadDecision=(*pEncoder)(InterCurrent, xi, CompressedFrame, 100);		
// write compressed speech frames to file
	    fwrite(CompressedFrame, sizeof(acelp_frame), 1, fprm);
	}

	fclose(fprm);
	fclose(fsample);

//  encoder deinitialization
	(*pDeInit)(InterCurrent);

	delete[] xi;
}